package brand

import (
	"context"
	"go_final/mxshop_api/goods_web/api"
	"go_final/mxshop_api/goods_web/forms"
	"go_final/mxshop_api/goods_web/global"
	goodspb "go_final/mxshop_api/goods_web/proto/protofile"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {
	pn := c.DefaultQuery("pn", "0")
	pnInt, _ := strconv.Atoi(pn)
	pSize := c.DefaultQuery("psize", "10")
	pSizeInt, _ := strconv.Atoi(pSize)

	res, err := global.GoodsClient.BrandList(context.Background(), &goodspb.BrandFilterRequest{
		Pages:       int32(pnInt),
		PagePerNums: int32(pSizeInt),
	})

	if err != nil {
		api.HandleGrpcError2Http(err, c)
		return
	}

	c.JSON(http.StatusOK, res)
}

func Create(c *gin.Context) {
	brandForm := forms.Brand{}
	err := c.ShouldBind(&brandForm)
	if err != nil {
		api.HandleValidateError(c, err)
		return
	}

	res, err := global.GoodsClient.CreateBrand(context.Background(), &goodspb.BrandRequest{
		Name: brandForm.Name,
		Logo: brandForm.Logo,
	})

	if err != nil {
		api.HandleGrpcError2Http(err, c)
		return
	}

	c.JSON(http.StatusOK, res)
}

func Delete(c *gin.Context) {
	brandID := c.Param("id")
	brandIDInt, _ := strconv.Atoi(brandID)

	res, err := global.GoodsClient.DeleteBrand(context.Background(), &goodspb.BrandRequest{
		Id: int32(brandIDInt),
	})

	if err != nil {
		api.HandleGrpcError2Http(err, c)
		return
	}

	c.JSON(http.StatusOK, res)
}

func Update(c *gin.Context) {
	brandID := c.Param("id")
	brandIDInt, _ := strconv.Atoi(brandID)

	brandForm := forms.Brand{}
	err := c.ShouldBind(&brandForm)
	if err != nil {
		api.HandleValidateError(c, err)
		return
	}

	res, err := global.GoodsClient.UpdateBrand(context.Background(), &goodspb.BrandRequest{
		Id:   int32(brandIDInt),
		Name: brandForm.Name,
		Logo: brandForm.Logo,
	})

	if err != nil {
		api.HandleGrpcError2Http(err, c)
		return
	}

	c.JSON(http.StatusOK, res)
}

func CategoryBrandList(c *gin.Context) {
	pn := c.DefaultQuery("pn", "0")
	pnInt, _ := strconv.Atoi(pn)
	pSize := c.DefaultQuery("psize", "10")
	pSizeInt, _ := strconv.Atoi(pSize)

	res, err := global.GoodsClient.CategoryBrandList(context.Background(), &goodspb.CategoryBrandFilterRequest{
		Pages:       int32(pnInt),
		PagePerNums: int32(pSizeInt),
	})

	if err != nil {
		api.HandleGrpcError2Http(err, c)
		return
	}

	c.JSON(http.StatusOK, res)
}

func DeleteCategoryBrand(c *gin.Context) {
	brandID := c.Param("id")
	brandIDInt, _ := strconv.Atoi(brandID)

	res, err := global.GoodsClient.DeleteCategoryBrand(context.Background(), &goodspb.CategoryBrandRequest{
		Id: int32(brandIDInt),
	})

	if err != nil {
		api.HandleGrpcError2Http(err, c)
		return
	}

	c.JSON(http.StatusOK, res)
}

func NewCategoryBrand(c *gin.Context) {
	categoryBrandForm := forms.CategoryBrandForm{}
	err := c.ShouldBind(&categoryBrandForm)
	if err != nil {
		api.HandleValidateError(c, err)
		return
	}

	res, err := global.GoodsClient.CreateCategoryBrand(context.Background(), &goodspb.CategoryBrandRequest{
		CategoryId: int32(categoryBrandForm.CategoryId),
		BrandId:    int32(categoryBrandForm.BrandId),
	})

	if err != nil {
		api.HandleGrpcError2Http(err, c)
		return
	}

	c.JSON(http.StatusOK, res)
}

func UpdateCategoryBrand(c *gin.Context) {
	categoryBrandID := c.Param("id")
	categoryBrandIDInt, _ := strconv.Atoi(categoryBrandID)

	categoryBrandForm := forms.CategoryBrandForm{}
	err := c.ShouldBind(&categoryBrandForm)

	if err != nil {
		api.HandleValidateError(c, err)
		return
	}

	res, err := global.GoodsClient.UpdateCategoryBrand(context.Background(), &goodspb.CategoryBrandRequest{
		Id:         int32(categoryBrandIDInt),
		CategoryId: int32(categoryBrandForm.CategoryId),
		BrandId:    int32(categoryBrandForm.BrandId),
	})

	if err != nil {
		api.HandleGrpcError2Http(err, c)
		return
	}

	c.JSON(http.StatusOK, res)
}

func GetCategoryBrandList(c *gin.Context) {
	categoryBrandID := c.Param("id")
	categoryBrandIDInt, _ := strconv.Atoi(categoryBrandID)

	res, err := global.GoodsClient.GetCategoryBrandList(context.Background(), &goodspb.CategoryInfoRequest{
		Id: int32(categoryBrandIDInt),
	})

	if err != nil {
		api.HandleGrpcError2Http(err, c)
		return
	}

	c.JSON(http.StatusOK, res)
}
